import User.people;
import User.User;
import User.Duser;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

//jdbc:h2:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1
//jdbc:h2:tcp://localhost/~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1

public class Treatment {
    public static final String PERSISTENCE_UNIT = "MyDSTest";

    //判断注册数据是否为空
    public static boolean islegitimate(String name, String email, String passwd, String passwd2)
    {
        if(name == null || email == null || passwd == null || passwd2 == null)
            return false;
        if(!passwd.equals(passwd2))
            return false;
        return true;
    }
    //判断邮箱是否已经被注册过
    public static boolean isRepeat(String email)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        User user = entityManager.find(User.class, email);
        entityManager.close();
        if(user == null)
            return true;
        else
            return false;
    }
    //判断用户邮箱和密码是否正确
    public static User isRight(String email, String passwd)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        User user = entityManager.find(User.class, email);
        entityManager.close();
        if(user != null)
        {
            if(user.getPasswd().equals(passwd))
                return user;
            else
                return null;
        }
        else
            return null;
    }
    //判断莞工id是否在本地注册/绑定过
    public static boolean isRight_dgut(String id)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Duser duser = entityManager.find(Duser.class, id);
        if(duser != null)
            return true;
        else
            return false;
    }
    //将注册好的用户信息写入文本
    public static boolean writeUser(String name, String email, String passwd)
    {
       EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
       EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        User user = new User(name, email, passwd, null, "userimags/0.jpg");
        try
        {
            entityTransaction.begin();
            entityManager.persist(user);
            entityTransaction.commit();
        }
        catch (Exception e)
        {
            System.out.println("写入用户到数据库失败！");
            return false;
        }
        entityManager.close();
        return true;
    }
    //为莞工登陆的用户创建账户并写入文本
    public static boolean writeDgut(Duser duser)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        try
        {
            entityTransaction.begin();
            entityManager.persist(duser);
            entityTransaction.commit();
        }
        catch (Exception e)
        {
            System.out.println("莞工登陆用户信息写入失败！");
            return false;
        }
        return true;
    }
    //上传用户图片
    public static boolean uploadimg(User user)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        User user1 = entityManager.find(User.class, user.getEmail());
        user1.setImg(user.getImg());
        try
        {
            entityTransaction.begin();
            entityManager.merge(user1);
            entityManager.flush();
            entityTransaction.commit();
        }
        catch (Exception e)
        {
            System.out.println("更改头像失败！");
            return false;
        }
        entityManager.close();
        return true;
    }
    public static boolean uploadimg2(Duser duser)
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        Duser duser1 = entityManager.find(Duser.class, duser.getId());
        duser1.setImg(duser.getImg());
        try
        {
            entityTransaction.begin();
            entityManager.merge(duser1);
            entityManager.flush();
            entityTransaction.commit();
        }
        catch (Exception e)
        {
            System.out.println("更改头像失败！");
            return false;
        }
        entityManager.close();
        return true;
    }
}
